﻿using System;
using System.Xml;
using System.Reflection;
using System.Collections.Generic;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.Administration;
using System.Globalization;
using CaveDigital.BoardMeeting2007Lite.ServerLib;
using System.Diagnostics;
using Microsoft.SharePoint.WebPartPages;
using System.Collections.Specialized;
using System.IO;

namespace CaveDigital.BoardMeeting2007Lite.CaveDigitalCSPDeploymentPackage
{
    class CaveDigitalCSPDeploymentPackage_Masterhandler : SPFeatureReceiver
    {
        private const string EVENT_PRODUCTNAME = "CaveDigital.BoardMeeting2007Lite";

		#region Overridden Methods (4) 

        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            SPSite caveDigitalMeetingsPortal = null;
            SPWeb caveDigitalWebPortal = null;
            try
            {
                if (properties.Feature.Parent != null && properties.Feature.Parent is SPSite)
                {
                    caveDigitalMeetingsPortal = properties.Feature.Parent as SPSite;
                    caveDigitalWebPortal = caveDigitalMeetingsPortal.OpenWeb();
                }
                else
                {
                    caveDigitalWebPortal = properties.Feature.Parent as SPWeb;
                    caveDigitalMeetingsPortal = caveDigitalWebPortal.Site;
                }
                Guid workflowGuid = new Guid("bf0b9ea0-7855-472f-8f82-b32605878ab7");
                Guid minutesGuid = new Guid("3fad61c5-2048-46fb-9f56-54eee77f191b");
                SPFeatureDefinition workflowFeature = SPFarm.Local.FeatureDefinitions[workflowGuid];
                SPFeatureDefinition minutesFeature = SPFarm.Local.FeatureDefinitions[minutesGuid];
                caveDigitalMeetingsPortal.Features.Add(minutesFeature.Id);
                caveDigitalMeetingsPortal.Features.Add(workflowFeature.Id);
            }
            catch (Exception ex)
            {
                System.Diagnostics.EventLog.WriteEntry(EVENT_PRODUCTNAME, "There was an error Activating The CaveDigital Board Meeting 2007 Lite." + ex.Message, System.Diagnostics.EventLogEntryType.Error);
            }
            finally
            {
                if (caveDigitalWebPortal != null)
                    caveDigitalWebPortal.Dispose();
                if (caveDigitalMeetingsPortal != null)
                    caveDigitalMeetingsPortal.Dispose();
            }
        }

        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
            SPSite caveDigitalMeetingsPortal = null;
            SPWeb caveDigitalWebPortal = null;
            try
            {
                if (properties.Feature.Parent != null && properties.Feature.Parent is SPSite)
                {
                    caveDigitalMeetingsPortal = properties.Feature.Parent as SPSite;
                    caveDigitalWebPortal = caveDigitalMeetingsPortal.OpenWeb();
                }
                else
                {
                    caveDigitalWebPortal = properties.Feature.Parent as SPWeb;
                    caveDigitalMeetingsPortal = caveDigitalWebPortal.Site;
                }
                Guid workflowGuid = new Guid("bf0b9ea0-7855-472f-8f82-b32605878ab7");
                Guid minutesGuid = new Guid("3fad61c5-2048-46fb-9f56-54eee77f191b");
                SPFeatureDefinition workflowFeature = SPFarm.Local.FeatureDefinitions[workflowGuid];
                SPFeatureDefinition minutesFeature = SPFarm.Local.FeatureDefinitions[minutesGuid];
                caveDigitalMeetingsPortal.Features.Remove(minutesFeature.Id);
                caveDigitalMeetingsPortal.Features.Remove(workflowFeature.Id);
            }
            catch (Exception ex)
            {
                System.Diagnostics.EventLog.WriteEntry(EVENT_PRODUCTNAME, "There was an error Deactivating The CaveDigital Board Meeting 2007 Lite." + ex.Message, System.Diagnostics.EventLogEntryType.Error);
            }
            finally
            {
                if (caveDigitalWebPortal != null)
                    caveDigitalWebPortal.Dispose();
                if (caveDigitalMeetingsPortal != null)
                    caveDigitalMeetingsPortal.Dispose();
            }
        }

        public override void FeatureInstalled(SPFeatureReceiverProperties properties){}

        public override void FeatureUninstalling(SPFeatureReceiverProperties properties){}

		#endregion Overridden Methods 

    }
}
